<%@ page import="java.net.URLDecoder" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.net.URLEncoder" %><%--
  Created by IntelliJ IDEA.
  User: TU
  Date: 2020/2/8
  Time: 16:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>


    <%
        //设置响应的消息体的数据格式以及编码
        response.setContentType("text/html;charset=utf-8");

        //1.获取所有cookie
        Cookie[] cookies = request.getCookies();
        boolean flag = false;//没有cookie为lastTime
        //2.遍历cookie数组
        if (cookies != null && cookies.length > 0) {

            for (Cookie cookie : cookies) {
                //3.获取cookie的名称
                String name = cookie.getName();
                //4.判断名称是否是: lastTime
                if ("lastTime".equals(name)) {
                    //有该cookie,不是第一次访问
                    flag = true;//有lastTime的cookie

                    //响应数据
                    //获取cookie的value, 时间
                    String value = cookie.getValue();
                    System.out.println("解码前:"+value);
                    //URL解码
                    value = URLDecoder.decode(value, "utf-8");
                    System.out.println("解码后:"+value);

    %>

                    <h1>欢迎回来,您上次访问的时间为:<%=value%></h1>

<%
                    //获取当前时间的字符串
                    Date date = new Date();
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                    String format = simpleDateFormat.format(date);
                    System.out.println("编码前:"+format);
                    //URL编码
                    format = URLEncoder.encode(format, "utf-8");
                    System.out.println("编码后:"+format);
                    cookie.setValue(format);
                    //设置cookie的存活时间
                    cookie.setMaxAge(60 * 60 * 24 * 30);//一个月
                    //设置cookie的value
                    response.addCookie(cookie);

                    break;

                }
            }

        }

        if (cookies == null || cookies.length == 0 || flag == false) {
            //没有lestTime,第一次访问
            //设置cookie的value
            //获取当前时间的字符串,重新设置cookie的值,重新发送cookie
            //获取当前时间的字符串
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            String format = simpleDateFormat.format(date);
            System.out.println("编码前:"+format);
            //URL编码
            format = URLEncoder.encode(format, "utf-8");
            System.out.println("编码后:"+format);
            Cookie lastTime = new Cookie("lastTime", format);
            //设置cookie的存活时间
            lastTime.setMaxAge(60 * 60 * 24 * 30);//一个月
            response.addCookie(lastTime);
    %>
    
            <h1>您好,欢迎您首次访问</h1>
    
    <%
        }
    %>

</body>
</html>
